<script type="text/javascript" src="lib/js/jquery.js"></script>
<script src="lib/js/elrte-1.2/js/jquery-ui-1.8.7.custom.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="lib/js/elrte-1.2/css/smoothness/jquery-ui-1.8.7.custom.css" type="text/css" media="screen" charset="utf-8">

<script src="lib/js/elrte-1.2/js/elrte.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="lib/js/elrte-1.2/css/elrte.min.css" type="text/css" media="screen" charset="utf-8">

<h1>Admin</h1>

<div style="border: 1px solid #999">
	<a href="index.php?action=admin&sub_action=inserisci">inserisci nuova pagina</a>
	<a href="index.php?action=admin">elenco</a>
</div>




<?php
require_once 'funzioni.php';

connettidb();

switch ($_REQUEST ['sub_action'])
{
	case 'elimina':eliminaPagina(); break;
	case 'modificadb':modificaPaginadb(); break;
	case 'modifica' : modificaPagina(); break;
	case 'inserisci' :visualizzaForm(); break;
	case 'inseriscidb' :inserisciPaginadb(); break;
	default:adminListPages(); break;
}	

function eliminaPagina()
{
	$sql="DELETE FROM pages WHERE id=$_GET[id]";
	//echo $sql;
	if(mysql_query($sql))
	
	{
		echo '<h2 class="msg">Dato eliminato</h2>';
		adminListPages();
	}
	else 
	
	{
		echo '<h2 class="msg">Errore</h2>';
	}
}

function modificaPaginadb()
{
	$sql= sprintf("UPDATE pages 
				  SET title_it='%s', title_en='%s',content_it='%s',content_en='%s',section='%s' 
				  WHERE id=%s",
	$_POST['title_it'],$_POST['title_en'],$_POST['content_it'],$_POST['content_en'],$_POST['section'],$_POST['id']
	
	);
	//echo $sql;
	if(mysql_query($sql))
		echo '<h2 class="msg">dati modificati</h2>';
	else
		echo '<h2 class="msg">errore</h2>';
}

function modificaPagina()
{
	echo $_GET['id'];
	$sql="SELECT *FROM pages WHERE id=$_GET[id]";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$dati=mysql_fetch_assoc($result);
		visualizzaForm($dati, 'modificadb');
	}
}


function inserisciPaginadb()
{
	$sql="INSERT INTO pages(title_it, title_en,content_it,content_en,section)
		VALUES ('$_POST[title_it]', '$_POST[title_en]',
		'$_POST[content_it]','$_POST[content_en]','$_POST[section]')";
	//echo $sql;
	mysql_query($sql);
	echo '<h2 class="msg>Dati inseriti</h2>';
}




function visualizzaForm($dati=array(), $sub_action='inseriscidb')
{
	include 'config.php';
	?>
	
	<form action="index.php?action=admin" method="post">
	<input type="hidden" name="sub_action" value='<?php echo $sub_action;?>'/>
	<input type="hidden" name="id" value='<?php echo $dati['id'];?>'/>
	
	<label>titolo (it)</label>
	<input type="text" name="title_it" value='<?php echo $dati['title_it'];?>'/><br />
	
	<label>titolo (en)</label>
	<input type="text" name="title_en" value='<?php echo $dati['title_en'];?>'/><br />
	
	<label>contenuto (it)</label>
	<textarea id="content_it" rows="5" cols="50" name="content_it"><?php echo $dati['content_it'];?></textarea><br />
	
	<label>contenuto (en)</label>
	<textarea id="content_en" rows="5" cols="50" name="content_en"><?php echo $dati['content_en'];?></textarea><br />
	
	<label>sezione</label>
	<select name="section">
	<?php 
		foreach($sections as $sezione=>$sottosezioni)
		{
			printf('<option value="%s" %s>%s</option>'
			,$sezione, verificaStato($sezione, $dati['section']),$sezione);  //%s sta per segnaposto
		}	
	
	
	?>
	</select>
	<input type="submit" value="invia" />
	
	
	</form>
	
	<script type="text/javascript">
	
	var opts = {
			cssClass : 'el-rte',
			// lang     : 'ru',
			height   : 150,
			toolbar  : 'eldorado',
			cssfiles : ['css/elrte-inner.css']
		}
	var opts2 = {
			cssClass : 'el-rte',
			// lang     : 'ru',
			height   : 150,
			toolbar  : 'eldorado',
			cssfiles : ['css/elrte-inner.css']
		}
		$('#content_it').elrte(opts);
	    $('#content_en').elrte(opts2);
	
	</script>
	
	
	
	<?php 
}

function verificaStato($valore,$valore2)
{
	if($valore==$valore2)
		return'selected="selected"';
		else 
			return '';
}


function adminListPages()
{
	$sql= "SELECT * FROM pages ORDER BY title_it ASC";
	$result = mysql_query($sql);
	if(mysql_num_rows($result))
	{
		echo '<table>
				<tr>
					<th>Id</th>
					<th>titolo</th>
					<th>Sezione</th>
					<th>Contenuto</th>
					<th>modifica</th>
					<th>elimina</th>
				</tr>';
		
		while ($row=mysql_fetch_assoc($result))
		{
			printf('<tr>
				   <th>%s</th>
				   <th>%s</th>
				   <th>%s</th>
				   <th>%s</th>
				   <th>%s</th>
				   <th>%s</th>
				   </tr>',$row['id'], $row['title_it'],$row['section'],substr($row['content_it'],0,50),
			'<a href="index.php?action=admin&sub_action=modifica&id='.$row['id'].'">modifica</a>',
			'<a class="delete" href="index.php?action=admin&sub_action=elimina&id='.$row['id'].'">elimina</a>');
		}
		echo'</table>';	
	?>
	<script type="text/javascript">
		$('.delete').click(function verifica(e)
		{
			//alert(122);
			if(confirm('sei sicuro di voler eliminare?'))
			{
			}
			else	
			e.preventDefault();
		});
	</script>
	<?php
	}
	
} 
	
	




